﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using UFSoft.UBF.Business;
using UFSoft.UBF.Eventing;
using UFSoft.UBF.Util.DataAccess;

namespace UFIDA.U9.Cust.XH.PlugBE
{
    /// <summary>
    /// 工作流 插件
    /// </summary>
    public class WorkFlowTaskBEExtend : IEventSubscriber
    {
        public void Notify(params object[] args)
        {
            if (args == null || args.Length == 0 || !(args[0] is UFSoft.UBF.Business.EntityEvent))
                return;
            BusinessEntity.EntityKey key = ((UFSoft.UBF.Business.EntityEvent)args[0]).EntityKey;
            if (key == null)
                return;
            UFIDA.U9.CS.Unify.TaskBE.TaskBE task = key.GetEntity() as UFIDA.U9.CS.Unify.TaskBE.TaskBE;
            if (task == null)
                return;
            string docType = string.Empty;   //不同业务单据的名称
            string formID = string.Empty;   //FORMid
            string entityID = string.Empty;  //单据信息ID
            string DocClass = string.Empty;  //单据类全称
            string Instance = string.Empty;   //InstanceID
            string SourceOrg = string.Empty;  //来源组织
            string ObjectOrg = string.Empty;   //目标组织
            UFIDA.U9.CS.Workflow.WorkflowInstanceBE.ProcessTrack pTrack = UFIDA.U9.CS.Workflow.WorkflowInstanceBE.ProcessTrack.Finder.FindByID(task.Source_Entity.ID);
            //D:\yonyou\U9CE\MobileService\UFSoft.UBF.MD.dll
            docType = pTrack.WaitingUser.FlowState.FlowInstance.OwnerEntity.MDEntity.DisplayName;
            entityID = pTrack.WaitingUser.FlowState.FlowInstance.OwnerEntity.ID.ToString();
            DocClass = pTrack.WaitingUser.FlowState.FlowInstance.OwnerEntity.Key.EntityType;
            Instance = pTrack.WaitingUser.FlowState.FlowInstance.Instance.ToString();
            SourceOrg = task.SourceOrg.ID.ToString();
            ObjectOrg = task.ObjectOrg.ID.ToString();
            #region 获取单据的提交人
            DataSet ds = new DataSet();
            string sql = $"select A1.Code from  Base_UserOrgRole as A  inner join Base_Role as A1 on(A.Role = A1.ID) inner join Base_UserOrg as A2 on(A.UserOrg = A2.ID) where(A2.[Org] = {ObjectOrg}) and(A2.[User] = {task.Sender.ID}) and A1.Code = 'IC103'";
            bool isRole = false;
            DataAccessor.RunSQL(DataAccessor.GetConn(), sql.ToString(), null, out ds); 
            if (ds != null && ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
            {
                isRole = true;
            }
            #endregion
            if (docType == "采购订单" && isRole)
            {
                //如果提交人的角色含有IC103 且审核结果为同意 且委外订单为审核中 则向OA推送单据委外订单
                UFIDA.U9.PM.PO.PurchaseOrder entity = UFIDA.U9.PM.PO.PurchaseOrder.Finder.FindByID(entityID);
                if (entity.Status == PM.PO.PODOCStatusEnum.Approving && task.Description.Contains("同意"))
                {
                    UFIDA.U9.Cust.XH.PlugBE.PurchaseOrderSubmitExtend.POToOA(entity);
                }

            }
        }
    }
}
